<template>
  <div style="display: flex;flex-direction: column">
    <!--按钮组和搜索框-->
    <div style="display: flex;flex-direction: row">
      <div style="margin: auto 23px">
        <el-button-group>
          <el-button @click="jumpToAdd" class="btnInGroup btnGroup" icon="el-icon-plus"></el-button>
        </el-button-group>
      </div>
      <div style="margin: 20px 10px 20px auto;">
        <el-input
            class="input"
            suffix-icon="el-icon-search"
            v-model="searchString"
            clearable
            @keydown.enter.native="search"
            prefix-icon="el-icon-office-building"
        />
        <el-button
            @click="search"
            style="width: 178px;height: 37px;border-radius: 4px;letter-spacing: 3px;color: var(--btnText)!important">
          根据院名搜索
        </el-button>
      </div>
    </div>
    <!--表格体部分-->
    <div v-loading="loading" style="height: 100%">
      <table>
        <tr style="color: var(--th);font-size: 14px">
          <th style="width: 1%"></th>
          <th align="left" class="thc" style="width: 5%">展示图片</th>
          <th align="left" class="thc" style="width: 10%">养老院名</th>
          <th align="left" class="thc" style="width: 10%">院联系电话</th>
          <th align="left" class="thc" style="width: 10%">特色服务</th>
          <th align="left" class="thc" style="width: 5%">价格</th>
          <th align="left" class="thc" style="width: 5%">评价</th>
          <th align="left" class="thc" style="width: 20%">地址</th>
          <th align="left" class="thc" style="width: 8%">建立时间</th>
          <th align="center" class="thc" style="width: 5%">操作</th>
        </tr>
        <tr style="height: 30px"></tr>
        <tr @contextmenu.prevent="openMenu($event,h)" class="items"
            v-for="(h,i) in homes" :key="i">
          <td></td>
          <td align="left">
            <el-avatar shape="square" :src="h.imageGallery"></el-avatar>
          </td>
          <td align="left">{{ h.name }}</td>
          <td align="left">{{ h.contact }}</td>
          <td align="left">{{ h.specialServices }}</td>
          <td align="left">{{ h.price }}</td>
          <td align="left">{{ h.appreciate }}</td>
          <td align="left">{{ h.address }}</td>
          <td align="left" v-if="h.createTime">{{ timeFormat(h.createTime) }}</td>
          <td align="left" v-else></td>
          <td align="center">
            <span @click.stop="openMenu($event,h)" class="el-icon-more" style="cursor: pointer;font-size: 20px"></span>
          </td>
        </tr>
      </table>
      <div v-if="homes.length === 0 && !loading">
        <div style="text-align: center;margin-top: 10%;font-size: 40px"><i class="el-icon-box"></i></div>
        <div style="text-align: center;margin-top: 20px;font-size: 20px">搜索结果为空</div>
        <div style="font-size: 10px;text-align: center;margin-top: 100px">请检查是否输入了正确的 '关键词'</div>
      </div>
      <div style="display: flex;width: 600px;position: absolute;bottom: 40px;right: calc((82% - 600px)/2)">
        <el-pagination
            v-if="!loading"
            style="display: inline-block;margin: auto"
            background
            layout="prev, pager, next"
            :total="total"
            :pager-count="11"
            :page-size="pageSize"
            :current-page="currentPage"
            @current-change="pageChange">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "CareHome",
  data() {
    return {
      //查询到的养老院数组
      homes: [],
      //搜索关键词
      searchString: '',
      //分页相关
      currentPage: 1,
      pageSize: 9,
      total: 0,
      //页面加载
      loading: false,
    }
  },
  methods: {
    //搜索
    load() {
      this.homes.forEach((h, index) => {
        if (h.contactId !== null) {
          //获取联系方式
          this.request.get('/details/get-contact/' + h.contactsId).then(res => {
            if (res.code === '200') {
              //成功
              this.homes[index].contact = res.data.phoneUmber
              this.$forceUpdate()

            } else if (res.code === '400') {
              this.homes[index].contact = '暂无联系方式'
              this.$forceUpdate()
            } else {
              this.$notify({
                type: 'error',
                title: '获取联系方式失败',
                message: res.msg
              })
            }
          }).catch(err => {
            this.$notify({
              type: 'error',
              title: '获取联系方式失败',
              message: err
            })
          })
        } else {
          this.homes[index].contact = '暂无联系方式'
          this.$forceUpdate()
        }

        if (h.priceDetails !== null) {
          //获取价格信息
          this.request.get('/details/get-price/' + h.priceDetails).then(res => {
            if (res.code === '200') {
              //成功
              this.homes[index].price = res.data.bedFee + res.data.nursingFee + res.data.foodFee + res.data.otherFee
              this.$forceUpdate()

            } else if (res.code === '400') {
              this.homes[index].price = '暂无价格'
              this.$forceUpdate()
            } else {
              this.$notify({
                type: 'error',
                title: '获取联系方式失败',
                message: res.msg
              })
            }
          }).catch(err => {
            this.$notify({
              type: 'error',
              title: '获取联系方式失败',
              message: err
            })
          })
        } else {
          this.homes[index].price = '暂无价格'
          this.$forceUpdate()
        }

        if (h.appreciateId !== null) {
          //获取评价信息
          this.request.get('/details/get-appreciate/' + h.appreciateId).then(res => {
            if (res.code === '200') {
              //成功
              this.homes[index].appreciate = res.data.grade
              this.$forceUpdate()
            } else if (res.code === '400') {
              this.homes[index].appreciate = '暂无评价'
              this.$forceUpdate()
            } else {
              this.$notify({
                type: 'error',
                title: '获取联系方式失败',
                message: res.msg
              })
            }
          }).catch(err => {
            this.$notify({
              type: 'error',
              title: '获取联系方式失败',
              message: err
            })
          })
        } else {
          this.homes[index].appreciate = '暂无评价'
          this.$forceUpdate()
        }

      })
    },
    search() {
      this.loading = true
      this.request.get('/details/search', {
        params: {
          pageSize: this.pageSize,
          pageNum: this.currentPage,
          keyword: this.searchString
        }
      }).then(res => {
        if (res.code === '200') {
          //成功
          this.homes = res.data.records
          this.total = parseInt(res.data.total)
          //根据属性中的一些id挨个查询
          this.load()
        } else {
          this.$notify({
            type: 'error',
            title: '查询失败',
            message: res.msg
          })
        }
      }).catch(err => {
        this.$notify({
          type: 'error',
          title: '查询失败',
          message: err
        })
      })
      this.loading = false
    },
    //换页
    pageChange(pageNum) {
      this.currentPage = pageNum
      this.search(false)
    },
    //打开右键菜单
    openMenu(e, user) {
      if (e.x > 1200) {
        if (e.y > 600) {
          this.$bus.$emit('showMenu', e.x - 200, e.y - 50, true, user)
        } else {
          this.$bus.$emit('showMenu', e.x - 200, e.y, true, user)
        }
      } else {
        if (e.y > 600) {
          this.$bus.$emit('showMenu', e.x, e.y - 50, true, user)
        } else {
          this.$bus.$emit('showMenu', e.x, e.y, true, user)
        }
      }
    },
    //格式化时间
    timeFormat(time) {
      if (time !== null) {
        return time.substring(0, 10)
      } else {
        return ''
      }
    },
    //跳转至添加页面
    jumpToAdd() {
      this.$router.push({
        path: '/home-data',
        query: {
          mode: 'add'
        }
      })
    }
  },
  mounted() {
    //进行查询
    this.search()
  },
}
</script>

<style scoped>
/*分割线*/
>>> .el-divider {
  background-color: var(--divider);
  width: 90%;
  height: 2px;
  margin: auto;
}

/*日期选择器样式*/
>>> .el-date-editor .el-input__inner {
  cursor: pointer;
}

/*按钮式单选框*/

>>> .el-radio-button__inner {
  background-color: var(--lightBg) !important;
  color: var(--deepText) !important;
  border: none !important;
}

>>> .is-active .el-radio-button__inner {
  background-color: var(--btn) !important;
  color: var(--btnText) !important;
  border: none !important;
  box-shadow: none;
}

/*表单label样式*/
>>> .el-form-item__label {
  color: var(--deepText);
}

>>> .el-form-item__content {
  margin-left: 100px !important;
}

/*表单按钮样式*/
.formBtn {
  color: var(--btnText);
  margin: 20px auto;
  width: 140px;
  height: 40px;
  font-size: 14px;
}

/*多选框样式*/
>>> .el-checkbox__inner {
  border: solid 1px var(--lightText);
}

>>> .el-checkbox__input.is-checked .el-checkbox__inner, .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  background-color: var(--btn) !important;
  border: none;
}

/*搜索按钮*/
>>> .el-button--default {
  font-size: 13px;
  font-weight: bold;
  background-color: var(--btn);
  color: var(--deepText);
  border: none;
  transition: 0.2s;
}

>>> .el-button--default:hover {
  background-color: var(--btnActive);
}

>>> .el-button--default::before {
  display: none;
}

/*下拉框样式*/
>>> .el-dropdown__caret-button {
  font-size: 13px;
  font-weight: bold;
  margin-left: 2px !important;
  color: var(--btnText);
}

>>> .el-dropdown-menu {
  background-color: var(--lightBg) !important;
}

>>> .popper__arrow, >>> .popper__arrow::after {
  border-bottom-color: var(--lightBg) !important
}

>>> .el-dropdown-menu__item {
  color: var(--deepText);
  font-weight: bold;
  padding: 10px 30px;
}

>>> .el-dropdown-menu__item:hover {
  background-color: var(--hover);
  color: var(--deepText);
}

>>> .el-dropdown > .el-button-group > .el-button:not(:last-child) {
  width: 148px;
}

/*按钮组样式*/
.btnGroup {
  background-color: var(--btn) !important;
  border: none;
  color: var(--btnText) !important;
  transition: 0.2s;
}

.btnGroup:hover {
  background-color: var(--btnActive) !important;
}

.btnInGroup {
  width: 60px;
  height: 30px;
  font-size: 16px;
  border-right: 2px solid var(--deepBg) !important;
  margin-bottom: 10px;
  padding: 5px;
}

/*弹窗样式*/
>>> .el-dialog {
  background-color: var(--lightBg);
  border-radius: 10px;
  height: 650px;
  width: 700px;
  margin-top: calc(25% - 300px) !important;
}

>>> .el-dialog__body {
  height: 100%;
  padding: 0;
}

>>> .el-dialog__header {
  height: 0;
  padding: 0;
}

>>> .el-dialog__wrapper {
  transition-duration: 0.3s;
}

>>> .dialog-fade-enter-active {
  animation: none !important;
}

>>> .dialog-fade-leave-active {
  transition-duration: 0.15s !important;
  animation: none !important;
}

>>> .dialog-fade-enter-active .el-dialog,
.dialog-fade-leave-active .el-dialog {
  animation-fill-mode: forwards;
}

>>> .dialog-fade-enter-active .el-dialog {
  animation-duration: 0.3s;
  animation-name: anim-open;
  animation-timing-function: cubic-bezier(0.6, 0, 0.4, 1);
}

>>> .dialog-fade-leave-active .el-dialog {
  animation-duration: 0.3s;
  animation-name: anim-close;
}

@keyframes anim-open {
  0% {
    opacity: 0;
    transform: scale3d(0, 0, 1);
  }
  100% {
    opacity: 1;
    transform: scale3d(1, 1, 1);
  }
}

@keyframes anim-close {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: scale3d(0.5, 0.5, 1);
  }
}

.thc {
  cursor: pointer;
}

.thc:hover {
  color: var(--deepText);
}

/*分页管理器样式*/
>>> .btn-quicknext, >>> .btn-quickprev, >>> .btn-next, >>> .btn-prev, >>> .number {
  color: var(--deepText) !important;
  background-color: var(--lightBg) !important;
}

>>> .btn-quicknext:hover, >>> .btn-quickprev:hover, >>> .btn-next:hover, >>> .btn-prev:hover, >>> .number:hover {
  background-color: var(--btnHover) !important;
}

>>> .active {
  background-color: var(--btn) !important;
  color: var(--btnText) !important;
}

>>> .active:hover {
  background-color: var(--btnActive) !important;
}

/*输入框样式*/
.input {
  margin-right: 20px;
  width: 400px;
  justify-content: center;
  align-items: center;
  border-radius: 5px;
}

>>> .el-input__inner {
  color: var(--deepText);
  background-color: var(--inputBg);
  border: solid 1px var(--inputBorder);
  border-radius: 5px;
}

>>> .el-date-editor .el-input__inner::placeholder {
  color: var(--lightText);
  padding-left: 10px;
}

>>> .el-input__icon {
  color: var(--deepText);
}

/*表格项样式*/
.items {
  width: 100%;
  font-size: 14px;
  height: 54px;
  border: solid #00000000;
  border-width: 10px 10px 10px 20px;
}

.items * {
  letter-spacing: normal;
}

.items:hover {
  background: var(--hover);
}

/*表格样式*/
table {
  width: 100%;
  color: var(--deepText);
  border-collapse: collapse;
  border: none;
}

/*表头行样式*/
tr td:first-child {
  border-bottom-left-radius: 12px;
  border-top-left-radius: 12px;
}

tr td:last-child {
  border-bottom-right-radius: 12px;
  border-top-right-radius: 12px;
}

>>> .el-input__inner:focus, >>> .el-textarea__inner:focus {
  border: var(--btn) 1px solid;
}
</style>